/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable semi */
/* eslint-disable indent */
/* eslint-disable @typescript-eslint/no-var-requires  */
/* eslint-disable no-undef */
const level1 = require('./src/asset/data/province.json');
const level2 = require('./src/asset/data/city.json');
const level3 = require('./src/asset/data/country.json');
const level4 = require('./src/asset/data/town.json');
const fs = require('fs');

const levelList = [level1, level2, level3, level4];

const loopChildren = (levelIndex, parentId) => {
    return Object.entries(levelList[levelIndex]).filter(([lastLevelId]) => parentId === lastLevelId).reduce((pre, [_, items]) => [...pre, ...items], []).map(item => {
        const current = {
            label: item.name,
            value: item.id,
        }
        if (levelList.length - 1 >= levelIndex + 1) {
            current.children = loopChildren(levelIndex + 1, item.id);
        }
        return current;
    })
}

const list = level1.map(level1Item => {
    return {
        label: level1Item.name,
        value: level1Item.id,
        children: loopChildren(1, level1Item.id)
    }
})

const jsonList = JSON.stringify(list, null, 4);
const writeStream = fs.createWriteStream('./src/asset/data/china.json');
writeStream.write(jsonList, (err) => {
    if (err) return console.warn(err.message);
    console.log('写入成功');
})